package cn.demomaster.quicklogger.sample;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.Arrays;
import java.util.List;

import cn.demomaster.quicklogger.LogLevel;
import cn.demomaster.quicklogger.QdLog;
import cn.demomaster.quicklogger.model.LogInfo;

public class MainActivity extends AppCompatActivity {

    Button btn_test;
    Handler handler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        requestPermissions();
        btn_test = findViewById(R.id.btn_test);
        btn_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                testError();
            }
        });
        handler.postDelayed(runnable,1000);
    }
    int index;
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            index++;
            handler.removeCallbacks(runnable);
            handler.postDelayed(runnable,5000);
            //QdLog.v("日志v"+index);
            //QdLog.d("日志d"+index);
            List<String> names = Arrays.asList("Alice", "Bob", "Alice", "Charlie");
            //QdLog.i(new int[]{1,2,3});
            QdLog.i("name",names);
            //LogInfo logInfo = new LogInfo(LogLevel.INFO, "test", "test", 1,"a","b","c",1,System.currentTimeMillis());
            //QdLog.i("logInfo",logInfo);
        }
    };
    private void testError() {
        int a = 1;
        int b = 0;
        try {
            int c = a/b;
        }catch (Exception e){
            QdLog.e(e);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        QdLog.w("onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        QdLog.w("onPause");
    }
    private void initLogger() {
    }

    int REQUEST_CODE = 10306;
    private void requestPermissions() {
        if (Build.VERSION.SDK_INT >= 23) {
            //此处做动态权限申请
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
        } else {
            //低于23 不需要特殊处理
            //initLogger();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[]
            grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE) {
            //当然权限多了，建议使用Switch，不必纠结于此
            if(grantResults.length>0) {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "权限申请成功", Toast.LENGTH_SHORT).show();
                    initLogger();
                } else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
                    Toast.makeText(this, "权限申请失败，用户拒绝权限", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(handler!=null) {
            handler.removeCallbacks(runnable);
        }
    }
}